#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu May 23 13:33:31 2019

@author: gloria

GRAPH A2
"""


# Select computer
computer = 'glgennaro' # work 
# computer = 'gloria' #personal



import pandas as pd
import os 
import matplotlib.pyplot as plt
import numpy as np

from matplotlib import rcParams

# figure size in inches
rcParams['figure.figsize'] = 11.7,8.27




os.chdir('/Users/' + computer + '/Dropbox/Progetti/Rhetoric/dta')
df = pd.read_stata('presidential_election_complete.dta')

# averages

df_f = df[['candidate', 'pop_dim2']]
df_f = df_f.groupby(['candidate']).mean()
df_f.columns = ['pop_mean']
df_s = df[['candidate', 'pop_w']]
df_s = df_s.groupby(['candidate']).std()
df_s.columns = ['pop_std']
df_t = df_f.join(df_s)
df_t['label'] = ['Clinton', 'Trump']

x = np.array(df_t.index)
y = np.array(df_t['pop_mean']) # Effectively y = x**2
e = np.array(df_t['pop_std'])
x_pos = np.arange(len(df_t.index))

fig, ax = plt.subplots()
ax.bar(x_pos, y, yerr=e, align='center', alpha=0.5, ecolor='black', capsize=7)
ax.set_ylabel('Populism', size=30)
ax.set_xticks(x_pos)
ax.set_xticklabels(df_t['label'], size=30, rotation=0 )
ax.yaxis.grid(True)
ax.tick_params(axis='y', which='major', labelsize=15);


os.chdir('/Users/' + computer + '/Dropbox/Progetti/Rhetoric/do/Replication package 2020 v3_2measures/Replication package 2020 v3/Graphs')
plt.savefig('pres_trump_clinton.png')

   





# plot over time
from matplotlib.ticker import FormatStrFormatter

x = df['date'].str.split(' ', n=3, expand = True)
x[0] = x[0].map(lambda y: y.strip(','))
x[1] = x[1].map(lambda y: y.strip(','))
x.columns = ['bo', 'bu', 'year']

bo = list(x['bo'])
bu = list(x['bu'])

day = []
month = []
for i in range(len(bo)):
    if bo[i].isdigit():
        day.append(bo[i])
        month.append(bu[i])
    else:
        day.append(bu[i])
        month.append(bo[i])

month_c = [0] * 226
for i in range(len(month)):
    if month[i] == 'January':
        month_c[i] = 1
    elif month[i]== 'February':
        month_c[i] = 2
    elif month[i]== 'March':
        month_c[i] = 3
    elif month[i]== 'April':
        month_c[i] = 4
    elif month[i]== 'May':
        month_c[i] = 5
    elif month[i]== 'June':
        month_c[i] = 6
    elif month[i]== 'July':
        month_c[i] = 7
    elif month[i]== 'August':
        month_c[i] = 8        
    elif month[i]== 'September':
        month_c[i] = 9        
    elif month[i]== 'October':
        month_c[i] = 10        
    elif month[i]== 'November':
        month_c[i] = 11       
    elif month[i]== 'December':
        month_c[i] = 12 
    else:
        continue


df['day'] = day
df['month'] = month
df['year'] = x['year']
df['month_c'] = month_c

df1 = df[df['year']=='2016']
df1 = df.sort_values('month_c')
#df['id'] = df.groupby(['month_c']).grouper.group_info[0]

df1 = df1.groupby(['candidate', 'month_c']).mean().reset_index()
df1 = df1[df1['month_c']>5]
df1 = df1[df1['month_c']<12]


df_trump = df1[df1['candidate']=='trump']
df_clinton= df1[df1['candidate']=='clinton']

import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes()
plt.plot(df_clinton['month_c'], df_clinton['pop_dim2'], label='Clinton', linestyle='-', color='black')
plt.plot(df_trump['month_c'], df_trump['pop_dim2'], label='Trump', linestyle='--', color='black')
plt.xlabel('Month Indicator', size=30)
plt.ylabel('Populism', size=30)
ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
ax.tick_params(axis='both', which='major', labelsize=15)
plt.legend(fontsize='xx-large');


os.chdir('/Users/' + computer + '/Dropbox/Progetti/Rhetoric/do/Replication package 2020 v3_2measures/Replication package 2020 v3/Graphs')
plt.savefig('pres_over_time.png')

   


